<template>
  <div class="edit">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{title}}</span>
        <el-button @click="$router.go(-1)" style="float: right; padding: 3px 0" type="text">返回</el-button>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="noticeName">
          <el-input v-model="form.noticeName" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="公告对象" prop="noticeScope">
          <el-select v-model="form.noticeScope" placeholder="请选择公告对象">
            <el-option label="所有用户" :value="1" />
            <el-option label="普通用户" :value="2" />
            <el-option label="推广员" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="内容详情" prop="noticeContent">
          <Editor v-model="form.noticeContent" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import Editor from "@/components/Editor";
import { getNotice, addNotice, updateNotice } from "@/api/system/notice";
export default {
  name: "NoticeEdit",
  components: {
    Editor
  },
  data() {
    return {
      title: "发布公告",
      // 表单参数
      form: {
        noticeName: "",
        noticeScope: "",
        noticeContent: ""
      },
      // 表单校验
      rules: {
        noticeName: [
          { required: true, message: "请输入文章标题", trigger: "blur" }
        ],
        noticeScope: [
          { required: true, message: "请选择公告对象", trigger: "change" }
        ],
        noticeContent: [
          { required: true, message: "请输入内容详情", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    this.getEditInfo();
  },
  methods: {
    getEditInfo() {
      const id = this.$route.query.id;
      console.log(id);
      if (id) {
        getNotice(id).then(response => {
          console.log(response);
          this.form = response.data;
        });
        this.title = "修改公告";
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const postData = {
            ...this.form,
            noticeContent: this.filterContent(this.form.noticeContent)
          };
          if (this.form.id != null) {
            updateNotice(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.$router.go(-1);
              }
            });
          } else {
            addNotice(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess("发布成功");
                this.$router.go(-1);
              }
            });
          }
        }
      });
    },
    // 重置按钮
    cancel() {
      this.getEditInfo();
      this.resetForm("form");
    }
  }
};
</script>
